package com.wsl.sale.logback;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
import org.slf4j.MDC;

/**
 * @Author:wangshuliang
 * @Description:  为layout添加自定义属性,声明属性及setter方法即可
 * @Date:Created in 18:18 2019/5/2
 * @Modified by:
 */
public class MySampleLayout2 extends LayoutBase<ILoggingEvent> {
    String prefix = null;
    boolean printThreadName = true;
    @Override
    public String doLayout(ILoggingEvent event) {
        StringBuffer stringBuffer = new StringBuffer();
        if (prefix !=null){
            stringBuffer.append(prefix+": ");
        }
        stringBuffer.append(event.getTimeStamp()-event.getLoggerContextVO().getBirthTime());
        stringBuffer.append("  ");
        stringBuffer.append(event.getLevel());
        if (printThreadName){
            stringBuffer.append(" [");
            stringBuffer.append(event.getThreadName());
            stringBuffer.append("] ");
        }else{
            stringBuffer.append(" ");
        }
        stringBuffer.append(event.getLoggerName());
        stringBuffer.append(" - ");
        stringBuffer.append(event.getFormattedMessage());
        stringBuffer.append(MDC.get("hello"));
        stringBuffer.append(CoreConstants.LINE_SEPARATOR);
        return stringBuffer.toString();
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public boolean isPrintThreadName() {
        return printThreadName;
    }

    public void setPrintThreadName(boolean printThreadName) {
        this.printThreadName = printThreadName;
    }
}
